var request = window.indexedDB.open('game-db', 1);
request.onupgradeneeded = function(event) {
	var db = event.target.result;
	var objectStore = db.createObjectStore('game-score', {
		keyPath: 'id'
	});
	var score = 100;
	var transaction = db.transaction(['game-score'], 'readwrite');
	var objectStore = transaction.objectStore('game-score');
	var request = objectStore.add({
		id: 1,
		score: score
	});
};


// // 打开数据库
// let request = window.indexedDB.open('gameDB', 1);

// // 定义数据存储对象
// let scoreStore = {
// 	name: 'scores',
// 	keyPath: 'id',
// 	autoIncrement: true,
// 	indexes: [{
// 		name: 'score',
// 		unique: false
// 	}]
// };

// // 创建数据存储对象
// request.onupgradeneeded = function() {
// 	let db = request.result;
// 	db.createObjectStore(scoreStore.name, scoreStore);
// };

function saveScore(score) {
	let transaction = db.transaction([scoreStore.name], 'readwrite');
	let store = transaction.objectStore(scoreStore.name);
	let request = store.add({
		score: score
	});

	request.onerror = function() {
		console.log('Error saving score to database.');
	};

	transaction.oncomplete = function() {
		console.log('Score saved to database.');
	};
}

function getScores(callback) {
	let transaction = db.transaction([scoreStore.name], 'readonly');
	let store = transaction.objectStore(scoreStore.name);
	let scores = [];

	let request = store.openCursor();

	request.onsuccess = function(event) {
		let cursor = event.target.result;
		if (cursor) {
			scores.push(cursor.value.score);
			cursor.continue();
		} else {
			callback(scores);
		}
	};

	request.onerror = function() {
		console.log('Error retrieving scores from database.');
	};
}